<?php
declare (strict_types=1);

namespace app\api\controller\v1;

use app\api\model\v1\AuditExam;
use app\api\model\v1\SpecialAuditExam;
use app\BaseController;
use think\Request;

class Special extends BaseController
{

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 专项案件
     */
    public function getSpecialExamList()
    {
        $limit = $this->request->param('limit', 10);
        $type_id = $this->request->param('type_id', 0);
        if (intval($type_id) === 0) {
            $res = SpecialAuditExam::with(['exam', 'unit'])->order('create_time desc')->paginate($limit);
        } else {
            $res = AuditExam::with(['exam', 'unit'])->where(['attribute' => 3])->order('create_time desc')->paginate($limit);
        }
        return $this->response($res);
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 专项案件列表
     */
    public function getSpecialFileList()
    {
        $special_audit_exam_id = $this->request->param('special_audit_exam_id');
        $type_id = $this->request->param('type_id', 0);
        if (intval($type_id) == 0) {
            $res = SpecialAuditExam::with(['exam' => ['examStairType' => ['examSecondLevel' => ['examDetail' => ['specialFile' => function ($query) use ($special_audit_exam_id) {
                $query->with(['user'])->where(['special_audit_exam_id' => $special_audit_exam_id]);
                return $query;
            }]]]], 'unit'])->find($special_audit_exam_id);
        } else {

            $res = AuditExam::with(['exam' => ['examStairType' => ['examSecondLevel' => ['examDetail' => ['auditExamFile.user' => function ($query) use ($special_audit_exam_id) {
                $query->with(['user'])->where(['audit_exam_id' => $special_audit_exam_id]);
                return $query;
            }]]]], 'unit'])->find($special_audit_exam_id);
            if (!empty($res['exam']['examStairType'])) {
                foreach ($res['exam']['examStairType'] as $key => $v) {
                    if(!empty($v['examSecondLevel'])){
                        foreach ($v['examSecondLevel'] as $key1 => $v1) {
                            if (!empty($v1['examDetail'])){
                                foreach ($v1['examDetail'] as $key2 => $v2) {
                                    if(!empty($v2['auditExamFile'])){
                                        $v2['auditExamFile']['arr_path'] = json_decode( $v2['auditExamFile']['file_path'],true);
                                    }
                                }
                            }
                        }
                    }
                }
            }

        }
        return $this->response($res->toArray());
    }
}
